﻿#region Using directives

using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;

#endregion

namespace Weazel.Toad.Application.Actions
{
  public class ConvertToVhdlAction : ActionBase
  {
    public ConvertToVhdlAction()
    {
      Enabled = false;

      Application.Instance.SelectedItemChanged += 
        new EventHandler<SelectedItemChangedEventArgs>(Instance_SelectedItemChanged);
    }

    public override void Execute(object sender, EventArgs e)
    {
      if (Application.Instance.SelectedItem is Items.GezelTextFile)
      {
        Items.GezelTextFile gezelFile =
          Application.Instance.SelectedItem as Items.GezelTextFile;

        List<Gezel.Model.Errors.ParseError> parseErrors =
          new List<Weazel.Gezel.Model.Errors.ParseError>();

        List<Gezel.Model.Errors.VerificationError> verificationErrors =
          new List<Weazel.Gezel.Model.Errors.VerificationError>();

        List<Gezel.Model.Errors.VerificationWarning> warnings =
          new List<Weazel.Gezel.Model.Errors.VerificationWarning>();

        Gezel.Model.Model model =
          gezelFile.ParseAndVerifyModel(
            ref parseErrors,
            ref verificationErrors,
            ref warnings
          );

        if (parseErrors.Count > 0 || verificationErrors.Count > 0)
        {
          // the model has errors, cannot convert
          string message =
            "Model contains error(s). Continue?.";

          DialogResult result =
            MessageBox.Show(
              message,
              "Warning",
              System.Windows.Forms.MessageBoxButtons.YesNo
            );

          if (result != DialogResult.Yes)
            return;
        }

        // no errors, or ignored by the user -> continue
        Application.Instance.GezelVhdlConversion.ConvertCurrentItem(model);
      }
    }

    void Instance_SelectedItemChanged(object sender, SelectedItemChangedEventArgs e)
    {
      if (e.SelectedItem is Items.GezelTextFile)
      {
        Enabled = true;
      }
      else
      {
        Enabled = false;
      }
    }
  }
}
